---
import { urlWithPrefix } from '@utils/urlWithPrefix';
import { DEFAULT_FRAMEWORK } from '@constants';
import type { Framework } from '@ag-grid-types';

interface Props {
    framework?: Framework;
    redirectPageName: string;
}

const { redirectPageName } = Astro.props as Props;
const redirectUrl = `./${redirectPageName}`;

const frameworkOverride = Astro.props.framework;
const defaultFramework = DEFAULT_FRAMEWORK;
---

<!-- Redirect for crawlers -->
<noscript>
    <meta
        http-equiv="refresh"
        content={`0;url=${urlWithPrefix({
            framework: frameworkOverride ?? defaultFramework,
            url: redirectUrl,
        })}`}
    />
</noscript>

<!-- 
    Redirect page to framework page based on stored internal framework

    NOTE: Need to put var into data attribute as Astro can't pass directly into build time JavaScript
-->
<div id="redirectUrl" data-framework-override={frameworkOverride} data-redirect-url={redirectUrl}></div>
<script>
    import { $internalFramework } from '@stores/frameworkStore';
    import { getFrameworkFromInternalFramework } from '@utils/framework';
    import { urlWithPrefix } from '@utils/urlWithPrefix';

    const data = document.getElementById('redirectUrl')?.dataset;
    const dataAttrFrameworkOverride = data.frameworkOverride;
    const dataAttrUrl = data.redirectUrl;
    const framework = dataAttrFrameworkOverride ?? getFrameworkFromInternalFramework($internalFramework.get());
    const redirectUrl = urlWithPrefix({ framework, url: dataAttrUrl });

    window.location.replace(redirectUrl);
</script>
